<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class employee extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->lang->load('emp');
        //$this->load->model('menu_model');
    }
    
    public function index()
    {
        $this->load->model('employee_model');
        if( !has_permission('KHVT.Employee.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('menu_employee') );
        $this->template->set_view('employee/content_home');
        
        $this->template->render();
    }
    
    public function datatable_employee(){
        $this->load->model('employee_model');
        $aaData = $this->employee_model->load_datatable_employee($_GET,1);
        $totalData = $this->employee_model->load_datatable_employee($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_employee($id = null){
        $this->load->model('employee_model');
        $data = array();

        if($id != null){
            $data['employee'] = $this->employee_model->getEmployeeById($id);
            $data['active'] = site_url('employee/edit');
        }else{
            $data['active'] = site_url('employee/add');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/employee/form_add_employee',$data);
    }
    
    public function add(){
        $this->load->model('employee_model');
        if( !has_permission('KHVT.Employee.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $code = trim( $this->input->post('code') );
            $name = trim( $this->input->post('name') );
            $cmnd_number = trim( $this->input->post('cmnd_number') );
            $cmnd_date = trim( $this->input->post('cmnd_date') );
            $cmnd_where = trim( $this->input->post('cmnd_where') );
            $birthday = trim( $this->input->post('birthday') );
            $phone = trim( $this->input->post('phone') );
            $address = trim( $this->input->post('address') );
            $cmnd_date = dateMysqlReplace($cmnd_date);
            $birthday = dateMysqlReplace($birthday);
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $code == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_01'),
                    'name' => 'code'
                );
            }else{
                if( $this->employee_model->unique_employee($code) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_02'),
                        'name' => 'code'
                    );
                }
            }
            
            if($name == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_03'),
                    'name' => 'name'
                );
            }
            
            if($cmnd_date == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_04'),
                    'name' => 'cmnd_date'
                );
            }else{
                if(!checkDateVn($cmnd_date)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_05'),
                        'name' => 'cmnd_date'
                    );
                }
            }
            
            if($birthday == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_06'),
                    'name' => 'birthday'
                );
            }else{
                if(!checkDateVn($birthday)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_07'),
                        'name' => 'birthday'
                    );
                }
            }
            
            if($cmnd_number == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_08'),
                    'name' => 'cmnd_number'
                );
            }
            if($cmnd_where == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_09'),
                    'name' => 'cmnd_where'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'code' => $code,
                    'name' => $name,
                    'cmnd_number' => $cmnd_number,
                    'cmnd_date' => dateVnToMysql($cmnd_date),
                    'cmnd_where' => $cmnd_where,
                    'birthday' => dateVnToMysql($birthday),
                    'phone' => $phone,
                    'address' => $address,
                    'status' => 1
                );
                
                $id = $this->employee_model->insertEmployee($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'action' => 'insert',
                'id' => $id
            );
            echo json_encode($data);
            
        }
    }
    public function edit(){
        $this->load->model('employee_model');
        if( !has_permission('KHVT.Employee.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $code = trim( $this->input->post('code') );
            $name = trim( $this->input->post('name') );
            $cmnd_number = trim( $this->input->post('cmnd_number') );
            $cmnd_date = trim( $this->input->post('cmnd_date') );
            $cmnd_where = trim( $this->input->post('cmnd_where') );
            $birthday = trim( $this->input->post('birthday') );
            $phone = trim( $this->input->post('phone') );
            $address = trim( $this->input->post('address') );
            $cmnd_date = dateMysqlReplace($cmnd_date);
            $birthday = dateMysqlReplace($birthday);
            
            $flag = true;
            $error = array();
            
            if( $code == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_01'),
                    'name' => 'code'
                );
            }else{
                if( $this->employee_model->unique_employee_edit($id,$code) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_02'),
                        'name' => 'code'
                    );
                }
            }
            
            if($name == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_03'),
                    'name' => 'name'
                );
            }
            
            if($cmnd_date == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_04'),
                    'name' => 'cmnd_date'
                );
            }else{
                if(!checkDateVn($cmnd_date)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_05'),
                        'name' => 'cmnd_date'
                    );
                }
            }
            
            if($birthday == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_06'),
                    'name' => 'birthday'
                );
            }else{
                if(!checkDateVn($birthday)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('emp_error_07'),
                        'name' => 'birthday'
                    );
                }
            }
            
            if($cmnd_number == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_08'),
                    'name' => 'cmnd_number'
                );
            }
            if($cmnd_where == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('emp_error_09'),
                    'name' => 'cmnd_where'
                );
            }
            /*
            if( $code == '' ){
                $flag = false;
                $message = lang('per_error_01');
                $error_code = 'validate';
            }else{
                if( $this->employee_model->unique_employee_edit($id,$code) == false ){
                    $flag = false;
                    $message = lang('emp_error_01');
                    $error_code = 'unique';
                }
            }
            
            if($name == ''){
                $flag = false;
                $message = lang('emp_error_01');
                $error_code = 'validate';
            }
            
            if($cmnd_date == ''){
                $flag = false;
                $message = lang('emp_error_01');
                $error_code = 'validate';
            }
            
            if($birthday == ''){
                $flag = false;
                $message = lang('emp_error_01');
                $error_code = 'validate';
            }
            
            if($cmnd_number == ''){
                $flag = false;
                $message = lang('emp_error_01');
                $error_code = 'validate';
            }
            */
            if($flag == true){
                $data_update = array(
                    'code' => $code,
                    'name' => $name,
                    'cmnd_number' => $cmnd_number,
                    'cmnd_date' => dateVnToMysql($cmnd_date),
                    'cmnd_where' => $cmnd_where,
                    'birthday' => dateVnToMysql($birthday),
                    'phone' => $phone,
                    'address' => $address
                );
                $this->employee_model->updateEmployee($data_update,$id);
            }
            $employee = $this->employee_model->getEmployeeById($id);
            $html = '';
            if($employee->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($employee->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($employee->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'code' => $employee->code,
                'name' => $employee->name,
                'cmnd_number' => $employee->cmnd_number,
                'cmnd_date' => dateMysqlToVN($employee->cmnd_date),
                'cmnd_where' => $employee->cmnd_where,
                'birthday' => dateMysqlToVN($employee->birthday),
                'phone' => $employee->phone,
                'address' => $employee->address,
                'status' => $html
            );
            echo json_encode($data);
            
        }
    }
    public function delete(){
        $this->load->model('employee_model');
        if( !has_permission('KHVT.Employee.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->employee_model->updateEmployee($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => lang('per_msg_01'),
            );
            echo json_encode($data);
        }
    }
    
    /*
    function list_permission($page = 1)
    {
        $this->load->model('roles_model');
        
        $itemsPerPage=20;
        $space=5;
        if($page == 0){$page=1;}
        
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->roles_model->getAllListPermission($itemsPerPage,$start);
        $totalItems = $this->roles_model->countAllListPermission();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/permission/ajax_list_permission',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function insert(){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Permission Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');

            }else{

                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $active = $this->input->post('active');
                
                $insertdata = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $active
                );
                $new_permissionId = $this->roles_model->insertPermission($insertdata);
                
                redirect('permission', 'location', 301);
            }
        }
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'permission'));
        $this->template->parse_view('content', 'default/permission/insert_permission', array(
            'action' => site_url('permission/insert'),
            'page_type' => 'insert'
        ));
       // Render template
        $this->template->render();
        
    }
    
    function edit($permissionId = 0){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $permission = $this->roles_model->getPermissionById($permissionId);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Role Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');
                
            }else{
                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $active = $this->input->post('active');
                
                $updatetdata = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $active
                );
                $this->roles_model->updatePermission($permissionId,$updatetdata);
                
                redirect('permission', 'location', 301);
            }
        }
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'roles'));
        $this->template->parse_view('content', 'default/permission/insert_permission', array(
            'action' => site_url('permission/edit/'.$permissionId),
            'page_type' => 'edit',
            'permission'=>$permission
        ));
       // Render template
        $this->template->render(); 
    }

    function setPermission($roleId, $permissionId, $status){
        $this->load->model('roles_model');
        //echo $status;
        switch($status){
            case 'set':
                $data_insert = array('role_id'=>$roleId,'permission_id'=>$permissionId);
                $this->roles_model->insertRolePermission($data_insert);
            break;
            
            case 'unset':
                $this->roles_model->deleteRolePermission($roleId, $permissionId);
            break;
        }
        
        $this->load->view('default/roles/ajax_change_permission',array('role_id'=>$roleId,'permission_id'=>$permissionId,'status'=>$status));
    }
    */
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */